from timeit import Timer

import sys
import math


def Problem():
    """
    145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.
    
    Find the sum of all numbers which are equal to the sum of the 
    factorial of their digits.
    
    Note: as 1! = 1 and 2! = 2 are not sums they are not included.
    """

    # Create factorial look up table from 0 to 9 (base 10 digits)
    factorial = lambda x: reduce(lambda y,z: y*z, range(1,x+1),1)
    facts = map(factorial, range(10))


    #Is curious number?
    curious = lambda x: sum(map(lambda y: facts[int(y)], str(x))) == x
    
    #find all curious numbers up to million
    nums = filter(curious, xrange(3, 7*facts[9]))
    print nums
    
    ans = sum(nums)
    print "Answer for Problem 34 = %d" % (ans,)
    
    
if __name__ == "__main__":
    t = Timer(setup='from __main__ import Problem', stmt='Problem()').timeit(1)
    print "Execution time = %0.3f seconds" %(t,)